#!/usr/bin/env python3
"""
401错误调试脚本
"""

import requests
import json

# 基础URL
BASE_URL = "http://localhost:8000"

def test_debug_login():
    """测试调试登录"""
    print("=== 测试调试登录 ===")
    
    data = {
        'username': 'test',
        'password': 'test'
    }
    
    try:
        response = requests.post(f"{BASE_URL}/accounts/debug-login/", json=data)
        print(f"状态码: {response.status_code}")
        print(f"响应头: {dict(response.headers)}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            print("✅ 调试登录成功")
            return True
        else:
            print("❌ 调试登录失败")
            return False
    except Exception as e:
        print(f"调试登录测试失败: {e}")
        return False

def test_original_login():
    """测试原始登录"""
    print("\n=== 测试原始登录 ===")
    
    data = {
        'username': 'test',
        'password': 'test'
    }
    
    try:
        response = requests.post(f"{BASE_URL}/accounts/login/", json=data)
        print(f"状态码: {response.status_code}")
        print(f"响应头: {dict(response.headers)}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            print("✅ 原始登录成功")
            return True
        else:
            print("❌ 原始登录失败")
            return False
    except Exception as e:
        print(f"原始登录测试失败: {e}")
        return False

def test_get_request():
    """测试GET请求"""
    print("\n=== 测试GET请求 ===")
    
    try:
        response = requests.get(f"{BASE_URL}/accounts/test/")
        print(f"状态码: {response.status_code}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            print("✅ GET请求成功")
            return True
        else:
            print("❌ GET请求失败")
            return False
    except Exception as e:
        print(f"GET请求测试失败: {e}")
        return False

def test_cors():
    """测试CORS"""
    print("\n=== 测试CORS ===")
    
    headers = {
        'Origin': 'http://localhost:8080',
        'Access-Control-Request-Method': 'POST',
        'Access-Control-Request-Headers': 'Content-Type'
    }
    
    try:
        response = requests.options(f"{BASE_URL}/accounts/debug-login/", headers=headers)
        print(f"状态码: {response.status_code}")
        print(f"CORS头: {dict(response.headers)}")
        
        if response.status_code == 200:
            print("✅ CORS预检成功")
            return True
        else:
            print("❌ CORS预检失败")
            return False
    except Exception as e:
        print(f"CORS测试失败: {e}")
        return False

def main():
    """主测试函数"""
    print("开始401错误调试...\n")
    
    # 测试GET请求
    get_success = test_get_request()
    
    # 测试调试登录
    debug_success = test_debug_login()
    
    # 测试原始登录
    original_success = test_original_login()
    
    # 测试CORS
    cors_success = test_cors()
    
    print(f"\n=== 调试结果 ===")
    print(f"GET请求: {'成功' if get_success else '失败'}")
    print(f"调试登录: {'成功' if debug_success else '失败'}")
    print(f"原始登录: {'成功' if original_success else '失败'}")
    print(f"CORS: {'成功' if cors_success else '失败'}")

if __name__ == "__main__":
    main() 